// order//order/orderCheck/index.js
const app = getApp();
const utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeInfo: {},
    productInfo: {},
    proList: [],
    orderInfo: {},
    checkNumber: 1,
    oid: 0,
    cateId: 0,
    count: 0//商品合计
  },

  getCouponInfo: function (oid = 0) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/order/GetOrderCheck?oid=' + oid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let orderInfo = res.data.Data;
            orderInfo.codesnList = [];
            let codelist = orderInfo.CodeSN.split(",");
            let lengthIndex = codelist.length;
            for (let m = lengthIndex - 1; m >= 0; m--) {
              orderInfo.codesnList.push(codelist[m]);
            }
            if (orderInfo.codesnList.length > 0) {
              let list = orderInfo.codesnList;
              for (let i = 0; i < list.length; i++) {
                list[i] = list[i].replace(/\s/g, '').replace(/(.{4})/g, "$1 ");
              }
              orderInfo.canUse = list.length - orderInfo.CheckCount;
            }
            that.setData({
              codeInfo: orderInfo
            })
          }
        }
      }
    })
  },

  numjian: function () {
    if (this.data.checkNumber > 1) {
      this.setData({
        checkNumber: this.data.checkNumber - 1
      })
    }

  },
  
  numjia: function () {
    if (this.data.checkNumber < this.data.codeInfo.canUse) {
      this.setData({
        checkNumber: this.data.checkNumber + 1
      })
    }
  },
  bindInput: function (e) {
    this.setData({
      checkNumber: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.q) {
      let param = decodeURIComponent(options.q);
      let uid = param.substring(param.indexOf('uid=') + 4, param.indexOf('&oid='));
      let oid = param.substring(param.indexOf('oid=') + 4, param.length);
      this.getCouponInfo(oid);
      this.getOrder(uid, oid);
      this.setData({
        oid: oid
      })
    }
  },

  getOrder: function (uid = 0, oid = 0) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Order/GetOrder?uid=' + uid + '&oid=' + oid,
      success: function (res) {
        if (res.data.Data) {
          let count = 0;
          let orderinfo = res.data.Data.OrderInfo;
          orderinfo.cutMoney = orderinfo.CardDiscountMoney > 0 ? orderinfo.CardDiscountMoney : (orderinfo.CardItemMoney > 0 ? orderinfo.CardItemMoney : orderinfo.CouponMoney);
          let proList = res.data.Data.OrderProductList;
          let cateId = proList[0].Info.CateId;
          for (let i = 0; i < proList.length; i++) {
            proList[i].optionName = '';
            if (proList[i].OptionList.length > 0) {
              for (let j = 0; j < proList[i].OptionList.length; j++) {
                proList[i].optionName += proList[i].OptionList[j].Name + '、';
              }
              proList[i].optionName = proList[i].optionName.substr(0, proList[i].optionName.length - 1);
            }
            if(proList[i].Info.ShowImg.indexOf('ps') != -1){
              proList[i].Info.ShowImg = utils.formatAvaterByProHead(proList[i].Info.ShowImg);
            }else{
              proList[i].Info.ShowImg = utils.formatAvater(proList[i].Info.ShowImg,'https://static.711688.net.cn/upload/store/'+ app.globalData.storeId +'/product/editor/');
            }
            count += proList[i].Info.BuyCount;
            if (proList[i].Info.cateId != 195) {
              proList[i].Info.ExpireTime = '';
              if (proList[i].Extend) {
                proList[i].Info.ExpireTime = proList[i].Extend.UseEndTime.substr(0, 10);
              }
            }
          }
          that.setData({
            orderInfo: orderinfo,
            proList: proList,
            cateId: cateId,
            count: count
          })
        }
      }
    })
  },

  submitCheck: function () {
    let that = this;
    let check = that.data.checkNumber;
    if (this.data.codeInfo.canUse < check) {
      wx.showModal({
        title: '提示',
        content: '代金券数量不足',
        showCancelL: false
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '是否确定核销?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Order/UpdateOrderCheck?oid=' + that.data.oid + '&checkUid=' + app.globalData.userInfo.Uid + '&checkCount=' + check,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '核销成功',
                  mask: true,
                  duration: 1000
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/home/index?isStartup=2',
                  })
                }, 1000)
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  cancelCheck: function () {
    wx.reLaunch({
      url: '/pages/home/index?isStartup=2',
    })
  },

  submitCheckPro: function () {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定核销?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Order/UpdateOrderCheck?oid=' + that.data.oid + '&checkUid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '核销成功',
                  mask: true,
                  duration: 1000
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/home/index?isStartup=2',
                  })
                }, 1500);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})